*------------------------------------------------------------------------------*
		*** DATA for FIGURES S14 and S15. SIMULATION OF PREDICTED CHANGE IN VOTE SHARE ***
*------------------------------------------------------------------------------*

***Prepare dataset for regressions

use election_date Land election_typeBWorLW kreis_code Kreis year PM10_30km PM10_30km_10ug Valid turnout *_share samplePM10_ll_allcontrols ///
	using "${tempdata}kreise_complete_all.dta", clear
sort election_date Land election_typeBWorLW kreis_code

keep if inrange(year, 2000, 2018)
keep if PM10_30km !=.
tostring year, replace	

merge m:1 kreis_code election_date using "${rawdata}/wind/wind_rb_d_20002019_iv.dta", keep(master match) nogen


**Assign wind directions to Hannover Landeshauptstadt and Saarbrucken Landeshauptstadt
*Hannover Landeshauptstadt
foreach el in 27sep2009 20jan2013 22sep2013 24sep2017 15oct2017 {
	foreach var of varlist iv_rb10_d1-iv_rb160_d4 {
		sum `var' if (kreis_code == 3241 & election_date == td(`el'))  // Region Hannover, Landkreis
		scalar `var'_3241_`el' = r(mean)
		replace `var' = `var'_3241_`el' if (kreis_code == 3241001 & election_date == td(`el'))
	} // var
} // el

*Saarbrucken Landeshauptstadt
foreach el in 25mar2012 22sep2013 26mar2017 24sep2017 {
	foreach var of varlist iv_rb10_d1-iv_rb160_d4 {
		sum `var' if (kreis_code == 10041 & election_date == td(`el'))  // Saarbrücken, Regionalverband
		scalar `var'_10041_`el' = r(mean)
		replace `var' = `var'_10041_`el' if (kreis_code == 10041100 & election_date == td(`el'))
	} // var
} // el

	
	
keep PM10_30km PM10_30km_10ug kreis_code Kreis election_date Land election_typeBWorLW Valid ///
	turnout *_share year samplePM10_ll_allcontrols iv_*


*------------------------------------------------------------------------------*
		
***Predictions based on regressions

reg Incumbent_share PM10_30km_10ug i.election_date i.kreis_code if samplePM10_ll_allcontrols==1
predict predOLSsimple1, xb

reg Incumbent_share i.election_date i.kreis_code if samplePM10_ll_allcontrols==1
predict predOLSsimple2, xb	

ivregress 2sls Incumbent_share  i.election_date i.kreis_code (PM10_30km_10ug=iv_*) if samplePM10_ll_allcontrols==1 
predict predIVsimple1, xb


save "${tempdata}kreise_closeelections.dta", replace

*------------------------------------------------------------------------------*

**Prepare dataset for dumbbell chart		
*Calculation for state elections
keep if election_typeBWorLW == "LW"

bys election_date Land: egen validtotal=total(Valid)
gen weight=Valid/validtotal
gen Incumbent_share_weight=weight*Incumbent_share*100

foreach party in CDU SPD GRUNE FDP DIELINKE AfD SonstigeParteien OtherParties STATTPARTEI SCHILL {
	gen ws_`party'=`party'_share*weight*100
}



replace Land=subinstr(Land, "_", "-",.)
gen electionname= Land + " " + year

egen electiongroup=group(election_date Land)
replace electiongroup=electiongroup+5

save "${tempdata}kreise_LW.dta", replace

*Calculation for federal elections		
use "${tempdata}kreise_closeelections.dta", clear
keep if election_typeBWorLW == "BW"

bys election_date: egen validtotal=total(Valid)
gen weight=Valid/validtotal
gen Incumbent_share_weight=weight*Incumbent_share*100

foreach party in CDU SPD GRUNE FDP DIELINKE AfD SonstigeParteien OtherParties STATTPARTEI SCHILL {
	gen ws_`party'=`party'_share*weight*100
}

egen electiongroup=group(election_date)	
gen electionname= "Federal Election" + " " + year
		
save "${tempdata}kreise_BW.dta", replace	

*Bring federal and state datasets together
append using "${tempdata}kreise_LW.dta"

bys electiongroup: gen nrobs=_N
bys electiongroup: egen nrincumbent=count(Incumbent_share)
	
gen predchangeOLS1=weight*(predOLSsimple1-predOLSsimple2)*100	
gen predchangeIV1=weight*(predIVsimple1-predOLSsimple2)*100	

destring year, replace
drop if electionname=="Hessen State 2009"

save "${tempdata}kreise_BWLW.dta", replace


**Collapse dataset	
collapse (sum) predchangeOLS* predchangeIV* weight ws_* Incumbent_share_weight (mean) electiongroup  year nrobs nrincumbent, by(electionname)
sort electiongroup


**Create share of other parties with seats; source for all: Wikipedia
	gen share_other_seats=0
	
	// SH: 5% threshold does not apply to SSW; Piraten sometimes with seats
	replace share_other_seats=4.1 if electionname=="Schleswig-Holstein 2000" // SSW 4.1%
	replace share_other_seats=3.6 if electionname=="Schleswig-Holstein 2005" // SSW 3.6%
	replace share_other_seats=4.3 if electionname=="Schleswig-Holstein 2009" // SSW 4.3%
	replace share_other_seats=12.8 if electionname=="Schleswig-Holstein 2012" // SSW 4.6%, Piraten 8.2%
	replace share_other_seats=3.3 if electionname=="Schleswig-Holstein 2017" // SSW 3.3%
	
	// NRW: Piraten 2012
	replace share_other_seats=7.8 if electionname=="Nordrhein-Westfalen 2012" // Piraten 7.8%

	 	
 	// MV: NPD 2006 and 2011
	replace share_other_seats=7.3 if electionname=="Mecklenburg-Vorpommern 2006" // NPD 7.3%
	replace share_other_seats=6.0 if electionname=="Mecklenburg-Vorpommern 2011" // NPD 6.0%

		
 	// BY: FW since 2008
	replace share_other_seats=10.2 if electionname=="Bayern 2008" // FW 10.2%
	replace share_other_seats=9.0 if electionname=="Bayern 2013" // FW 9.0%
	replace share_other_seats=11.6 if electionname=="Bayern 2018" // FW 11.6% 
	
	// Saarland: Piraten 2012
	replace share_other_seats=7.4 if electionname=="Saarland 2012" // Piraten 7.4%
	
	// Brandenburg: BVB/FW
	replace share_other_seats=2.7 if electionname=="Brandenburg 2014" // BVB/FW 2.7
	replace share_other_seats=5.0 if electionname=="Brandenburg 2019" // BVB/FW 5.0
	
	
	// Sachsen: NPD
	replace share_other_seats=9.2 if electionname=="Sachsen 2004" // NPD 9.2%
	replace share_other_seats=5.6 if electionname=="Sachsen 2009" // NPD 5.6%
	
	gen AfDover5=0
	replace AfDover5=ws_AfD if ws_AfD>5
	
	gen SCHILLover5=0
	replace SCHILLover5=ws_SCHILL if ws_SCHILL>5
	
foreach party in GRUNE FDP DIELINKE {
	gen `party'_below5=0
	replace `party'_below5=ws_`party' if ws_`party'<5 
}	

	gen sharenoseats = ws_OtherParties - SCHILLover5 - AfDover5 - share_other_seats ///
			+ GRUNE_below5 + FDP_below5 + DIELINKE_below5
			
	gen share_opposition = 100- sharenoseats - 	Incumbent_share_weight
	gen factornoseats = sharenoseats/(Incumbent_share_weight + share_opposition)
	gen incumbent_final = Incumbent_share_weight*(1+factornoseats)
	
	
**See if pollution made a difference for the incumbent share
gen share_woPM_OLS1 = Incumbent_share_weight-predchangeOLS1
gen share_woPM_IV1 = Incumbent_share_weight-predchangeIV1
gen share_woPM_IV1_seats = incumbent_final-predchangeIV1
	

gen Incshare50 = Incumbent_share_weight > 50

gen Incshare50PM_OLS1 = share_woPM_OLS1 >50
gen Incshare50PM_IV1  = share_woPM_IV1 >50

gen PMdiffOLS1=Incshare50 - Incshare50PM_OLS1	
gen PMdiffIV1=Incshare50 - Incshare50PM_IV1

count if PMdiffOLS1==1
scalar changeOLS=r(N)

count if PMdiffIV1==1
scalar changeIV=r(N)

save "${tempdata}kreise_elections_collapsed.dta", replace



*------------------------------------------------------------------------------*

clear

exit
